//
//  SSOrderViewController.swift
//  拾舍清洁
//
//  Created by sky.wei on 2023/2/6.
//

import UIKit

import SwiftyJSON
import ESPullToRefresh

class SSOrderViewController: UIViewController {
    
    
    @IBOutlet weak var tableView: SSBaseTableView!
    
    var items:Array<SSOrder> = []
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUIs()
//        network()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        network()
    }
    
    func setupUIs() {
        tableView.register(UINib(nibName: "SSOrderTableViewCell", bundle: Bundle.init(for: SSOrderTableViewCell.self)), forCellReuseIdentifier: "SSOrderTableViewCell")
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.nullView.textLab.text = "暂无订单"
        self.tableView.es.addPullToRefresh {
            [unowned self] in
            self.tableView.es.stopPullToRefresh()

//            self.tableView.es.stopPullToRefresh(completion: true)
            /// Set ignore footer or not
//            self.tableView.es.stopPullToRefresh(completion: true, ignoreFooter: false)
            network()
            print("1233")
        }
    }
    
    func network() {
        let param = [
            "pageCurrent": "1",
              "pageSize": "20",
              "phoneNo": kUserPhone
        ] as [String:AnyObject]
        SSProgressHUD.share.show()
        HttpConnectTools.share.request(urlString: APIUrl.share.getOrder_URL, parameters: param) { (result, error) in
            SSProgressHUD.share.dismiss()
            if let _ = result{
                self.items.removeAll()
                let json = JSON(parseJSON: result!)
                let datas = json["data"]["listOrder"].arrayValue
                for item in datas {
                    let order = SSOrder()
                    order.itemNames = item["itemNames"].stringValue
                    order.orderNo = item["orderNo"].stringValue
                    order.total = item["total"].stringValue
                    order.serviceTime = item["serviceTime"].stringValue
                    self.items.append(order)
                }
                self.tableView.isHiddenNullView(isHiden: !(self.items.count == 0))
                self.tableView.reloadData()
            }else{
//                if let message = error?.userInfo["errorinfo"]{
//                    self.view.makeToast(message as? String)
//                }
            }
        }
    }
    

}
extension SSOrderViewController: UITableViewDelegate, UITableViewDataSource
{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//        let item :ListCombo =  self.items[indexPath.section].listCombos[indexPath.row] as! ListCombo
        let vc =  SSOrderDetailController.init(nibName: "SSOrderDetailController", bundle: nil)
        vc.orderNo = self.items[indexPath.row].orderNo
        navigationController?.pushViewController(vc, animated: true)
    }
  
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SSOrderTableViewCell") as! SSOrderTableViewCell
        cell.setData(order: self.items[indexPath.row])
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 148
    }
 
}


